import { LoginByPhone, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { notEqual } from 'assert'

const user = {
  state: {
    user: {}
  },

  mutations: {
    SET_USER: (state, user) => {
      
      state.user = user
    },
  },

  actions: {
    // 用户名登录
    LoginByPhone({ commit }, userInfo) {
      const user = { 
        name:"建安",
        account:"t4h5sgiupl4v",
        phone: "139929889050",
        height: 173,
        priKey: 'PVT_SM2_2cNgM82WYYLm9zEQKxaBD2iaXhT4URirPSzQpECwZX35jWH9Gd',
        pubKey: 'PUB_SM2_7hmqEGoDe48QZQBT3ukvXJppoivyG67UUzwMLz4Lewt5zgNBNV',

      }
      commit('SET_USER', user)
      return true
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(response => {
          if (!response.data) { // 由于mockjs 不支持自定义状态码只能这样hack
            reject('error')
          }
          const { data } = response.data
          data.roles = [data.role]
          console.log(data)
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_USER', data._id)
          commit('SET_NAME', data.account)
          commit('SET_CODE', data.note)
          commit('SET_INVITE', data.invite)
          commit('SET_CAR', data.career)
          commit('SET_INTRODUCTION', data.introduction)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_CODE', data.note)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
